home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / misc / maplay1_2.lha / maplay / header.cc < prev    next >
C/C++ Source or Header  |  1994-06-23  |  7KB  |  250 lines

  1. /*
  2.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  *  Changes from version 1.1 to 1.2:
  23.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  24.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  25.  *    - syncword recognition slightly changed
  26.  */
  27.  
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. #include <iostream.h>
  31. #include <iomanip.h>
  32. #include <stdlib.h>
  33. #include "header.h"
  34.  
  35.  
  36. const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };
  37.  
  38.  
  39. bool Header::read_header (Ibitstream *stream, Crc16 **crcp)
  40. {
  41.   uint32 headerstring;
  42.  
  43.   if (!stream->get_header (&headerstring))
  44.     return False;
  45.  
  46.   if ((headerstring & 0xFFF80000) != 0xFFF80000)
  47.   {
  48.     cerr << "invalid syncword 0x";
  49.     cerr.width (8);
  50.     cerr.fill ('0');
  51.     cerr << hex << headerstring
  52.      << " found at fileoffset " << dec
  53.      << lseek (stream->filedescriptor (), 0, SEEK_CUR) - 4 << '\n';
  54.     return False;
  55.   }
  56.  
  57.   if ((h_layer = (headerstring >> 17) & 3) == 0)
  58.   {
  59.     cerr << "unknown layer identifier found!\n";
  60.     exit (1);
  61.   }
  62.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III
  63.   if (h_layer == 3)
  64.   {
  65.     cerr << "Sorry, Layer III not implemented!\n";
  66.     exit (1);
  67.   }
  68.   h_protection_bit = (headerstring >> 16) & 1;
  69.   if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  70.   {
  71.     cerr << "unknown bitrate index found!\n";
  72.     exit (1);
  73.   }
  74.   if (!h_bitrate_index)
  75.   {
  76.     cerr << "free format not yet implemented!\n";
  77.     exit (1);
  78.   }
  79.  
  80.   if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  81.   {
  82.     cerr << "unknown sample frequency!\n";
  83.     exit (1);
  84.   }
  85.   h_padding_bit = (headerstring >> 9) & 1;
  86.   h_mode = (e_mode)((headerstring >> 6) & 3);
  87.   if (h_layer == 2)
  88.     // testing validity of mode and bitrate:
  89.     if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  90.      h_mode != single_channel) ||
  91.     (h_bitrate_index >= 11 && h_mode == single_channel))
  92.     {
  93.       cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  94.           "  mode: " << mode_string ()
  95.        << "\n  bitrate: " << bitrate_string () << '\n';
  96.       exit (1);
  97.     }
  98.   h_mode_extension = (headerstring >> 4) & 3;
  99.   if (h_mode == joint_stereo)
  100.     h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  101.   else
  102.     h_intensity_stereo_bound = 0;        // should never be used
  103.   h_copyright = (headerstring >> 3) & 1;
  104.   h_original = (headerstring >> 2) & 1;
  105.  
  106.   // calculate number of subbands:
  107.   if (h_layer == 1)
  108.     h_number_of_subbands = 32;
  109.   else
  110.   {
  111.     uint32 channel_bitrate = h_bitrate_index;
  112.  
  113.     // calculate bitrate per channel:
  114.     if (h_mode != single_channel)
  115.       if (channel_bitrate == 4)
  116.     channel_bitrate = 1;
  117.       else
  118.     channel_bitrate -= 4;
  119.  
  120.     if (channel_bitrate == 1 || channel_bitrate == 2)
  121.       if (h_sample_frequency == thirtytwo)
  122.     h_number_of_subbands = 12;
  123.       else
  124.     h_number_of_subbands = 8;
  125.     else
  126.       if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
  127.     h_number_of_subbands = 27;
  128.       else
  129.     h_number_of_subbands = 30;
  130.   }
  131.  
  132.   if (h_intensity_stereo_bound > h_number_of_subbands)
  133.     h_intensity_stereo_bound = h_number_of_subbands;
  134.  
  135.   // read framedata:
  136.   if (!stream->read_frame (calculate_framesize ()))
  137.     return False;
  138.  
  139.   if (!h_protection_bit)
  140.   {
  141.     // frame contains a crc checksum
  142.     checksum = (uint16)stream->get_bits (16);
  143.     if (!crc)
  144.       crc = new Crc16;
  145.     crc->add_bits (headerstring, 16);
  146.     *crcp = crc;
  147.   }
  148.   else
  149.     *crcp = (Crc16 *)0;
  150.  
  151.   return True;
  152. }
  153.  
  154.  
  155. uint32 Header::calculate_framesize ()
  156. /* calculates framesize in bytes excluding header size */
  157. {
  158.   static const int32 bitrates_layer_1[15] = {
  159.     0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  160.     224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000 };
  161.   static const int32 bitrates_layer_2[15] = {
  162.     0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  163.     112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000 };
  164.   static const samplefrequencies[3] = { 44100, 48000, 32000 };
  165.   uint32 framesize;
  166.  
  167.   if (h_layer == 1)
  168.   {
  169.     framesize = (12 * bitrates_layer_1[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  170.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  171.       ++framesize;
  172.     framesize <<= 2;        // one slot is 4 bytes long
  173.   }
  174.   else
  175.   {
  176.     framesize = (144 * bitrates_layer_2[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  177.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  178.       ++framesize;
  179.   }
  180.  
  181.   return framesize - 4;        // subtract header size
  182. }
  183.  
  184.  
  185. const char *Header::layer_string (void)
  186. {
  187.   switch (h_layer)
  188.   {
  189.     case 1:
  190.       return "I";
  191.     case 2:
  192.       return "II";
  193.     case 3:
  194.       return "III (not implemented!)";
  195.   }
  196.   return NULL;            // dummy
  197. }
  198.  
  199.  
  200. const char *Header::bitrate_string (void)
  201. {
  202.   static const char *layer1_bitrates[16] = {
  203.     "free format", "32 kbis/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s", 
  204.     "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
  205.     "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"
  206.   };
  207.   static const char *layer2_bitrates[16] = {
  208.     "free format", "32 kbis/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", 
  209.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
  210.     "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"
  211.   };
  212.  
  213.   if (h_layer == 1)
  214.     return layer1_bitrates[h_bitrate_index];
  215.   else
  216.     return layer2_bitrates[h_bitrate_index];
  217. }
  218.  
  219.  
  220. const char *Header::sample_frequency_string (void)
  221. {
  222.   switch (h_sample_frequency)
  223.   {
  224.     case thirtytwo:
  225.       return "32 kHz";
  226.     case fourtyfour_point_one:
  227.       return "44.1 kHz";
  228.     case fourtyeight:
  229.       return "48 kHz";
  230.   }
  231.   return NULL;            // dummy
  232. }
  233.  
  234.  
  235. const char *Header::mode_string (void)
  236. {
  237.   switch (h_mode)
  238.   {
  239.     case stereo:
  240.       return "stereo";
  241.     case joint_stereo:
  242.       return "joint stereo";
  243.     case dual_channel:
  244.       return "dual channel";
  245.     case single_channel:
  246.       return "single channel";
  247.   }
  248.   return NULL;            // dummy
  249. }
  250.